import re
from django.conf import settings

class Compiler(object):

    def compile(self, record):
        #todo: need to be refactored
        record.compiled_story = record.story.replace('\n', '<br/>')

        # VK
        while("[vk]" in record.compiled_story):
		    tag_vk = re.compile("\[vk\](?P<content>.+?)\[/vk\]")
		    record.compiled_story = re.sub(tag_vk, "\\1", record.compiled_story)

        # LEFT
        while("[left]" in record.compiled_story):
		    tag_left = re.compile("\[left\](?P<content>.+?)\[/left\]")
		    record.compiled_story = re.sub(tag_left, "<div class='align-left'>\\1</div>", record.compiled_story)

        # RIGHT
        while("[right]" in record.compiled_story):        
		    tag_right = re.compile("\[right\](?P<content>.+?)\[/right\]")
		    record.compiled_story = re.sub(tag_right, "<div class='align-right'>\\1</div>", record.compiled_story)

        # CENTER
        while("[center]" in record.compiled_story):
		    tag_center = re.compile("\[center\](?P<content>.+?)\[/center\]")
		    record.compiled_story = re.sub(tag_center, "<div class='align-center'>\\1</div>", record.compiled_story)

        # COLOR
        while("[color=" in record.compiled_story):
		    tag_color = re.compile("\[color=#([A-Za-z0-9]+)\](?P<content>.+?)\[/color\]")
		    record.compiled_story = re.sub(tag_color, "<span style='color: #\\1'>\\2</span>", record.compiled_story)

		    tag_color = re.compile("\[color=([A-Za-z]+)\](?P<content>.+?)\[/color\]")
		    record.compiled_story = re.sub(tag_color, "<span style='color: \\1'>\\2</span>", record.compiled_story)


        # SIZE
        while("[size=" in record.compiled_story):
		    tag_size = re.compile("\[size=([0-9])\](?P<content>.+?)\[/size\]")
		    record.compiled_story = re.sub(tag_size, "<span style='font-size: 1.\\1em'>\\2</span>", record.compiled_story)

        # B
        while("[b]" in record.compiled_story):
		    tag_b = re.compile("\[b\](?P<content>.+?)\[/b\]")
		    record.compiled_story = re.sub(tag_b, "<strong>\\1</strong>", record.compiled_story)

        # I
        while("[i]" in record.compiled_story):
		    tag_i = re.compile("\[i\](?P<content>.+?)\[/i\]")
		    record.compiled_story = re.sub(tag_i, "<em>\\1</em>", record.compiled_story)

        # U
        while("[u]" in record.compiled_story):
		    tag_u = re.compile("\[u\](?P<content>.+?)\[/u\]")
		    record.compiled_story = re.sub(tag_u, """<span class="underlined">\\1</span>""", record.compiled_story)

        # URL
        tag_url = re.compile("\[url\s*=\s*(\S.+?)\s*\](.*?)\[\/url\]")
        record.compiled_story = re.sub(tag_url, """<a href="\\1">\\2</a>""", record.compiled_story)

        # VIDEO
        tag_video = re.compile("\[video=(?P<url>.+?)\]")
        record.compiled_story = re.sub(
	        tag_video,
	        """
	        <object id="videoplayer%(pk)s" type="application/x-shockwave-flash" data="%(url)s/static/uppod/uppod.swf" width="500" height="375">
	        <param name="bgcolor" value="#ffffff" />
	        <param name="allowFullScreen" value="true" />
	        <param name="movie" value="%(url)s/static/uppod/uppod.swf" />
	        <param name="flashvars" value="comment=&amp;m=video&amp;file=\\1" />
	        </object>
	        """ % {
	            'url': settings.LIVE_ENGINE_URL,
	            'pk': record.pk
	        },
	        record.compiled_story
	    )

        # YOUTUBE
        tag_youtube = re.compile("\[youtube=([^\]]+)\?v=([a-zA-Z0-9]+)(|[^\]]+)\]")
        record.compiled_story = re.sub(
            tag_youtube,
            """
            <object width="420" height="315"><param name="movie" value="http://www.youtube.com/v/\\2?version=3&amp;hl=ru_RU"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/\\2?version=3&amp;hl=ru_RU" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object>
            """,
            record.compiled_story
        )


         # IMG
        tag_img = re.compile("\[img\](?P<content>.+?)\[/img\]")
        record.compiled_story = re.sub(
            tag_img,
            """<img src="\\1" border="0" alt="%(title)s" />""" % {
                'title': record.title
            },
            record.compiled_story
        )
        tag_img = re.compile("\[img=(.+?)\](.+?)\[/img\]")
        record.compiled_story = re.sub(
            tag_img,
            """<img src="\\2" aling="\\1" border="0" alt="%(title)s" />""" % {
                'title': record.title
            },
            record.compiled_story
        )

        # THUMB
        tag_thumb = re.compile("\[thumb\]([^\[]*)([/\\\\])(.*?)\[/thumb\]")
        record.compiled_story = re.sub(
            tag_thumb,
            """<a href="\\1\\2\\3" onclick="return hs.expand(this)" title="%(title)s"><img src="\\1/thumbs\\2\\3" border="0" alt="%(title)s" /></a>""" % {'title': record.title},
            record.compiled_story
        )
        tag_thumb = re.compile("\[thumb=(.*?)\]([^\[]*)([/\\\\])(.*?)\[/thumb\]")
        record.compiled_story = re.sub(
            tag_thumb,
            """<a href="\\2\\3\\4" onclick="return hs.expand(this)" title="%(title)s"><img src="\\2/thumbs\\3\\4" border="0" alt="%(title)s" /></a>""" % {'title': record.title},
            record.compiled_story
        )

        tag_remote_thumb = re.compile("\[rthumb=(.+?)\](.+?)\[/rthumb\]")
        record.compiled_story = re.sub(
            tag_remote_thumb,
            """<a href="\\1" onclick="return hs.expand(this)" title="%(title)s"><img src="\\2" border="0" alt="%(title)s" /></a>""" % {'title': record.title},
            record.compiled_story
        )

        # FLASH
        tag_flash = re.compile("\[flash=([0-9]+),([0-9]+)\](.+?)\[/flash\]")
        record.compiled_story = re.sub(
            tag_flash,
            """
            <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='\\1' height='\\2'>
            <param name='movie' value='\\3'>
            <param name='wmode' value='transparent' />
            <param name='play' value='true'>
            <param name='loop' value='true'>
            <param name='quality' value='high'>
            <param name='allowscriptaccess' value='never'>
            <embed AllowScriptAccess='never' src='\\3' width='\\1' height='\\2' play='true' loop='true' quality='high' wmode='transparent'>
            </embed>
            </object>
            """,
            record.compiled_story
        )


        # EMOTICONS
        tag_emoticon = re.compile(":([a-zA-Z0-9_]+):")
        record.compiled_story = re.sub(
            tag_emoticon,
            """<img src="%s/static/images/emoticons/\\1.gif" border="0" alt="\\1" class="emoticon" />""" % settings.LIVE_ENGINE_URL,
            record.compiled_story
        )

        return record
